package lang;

import org.junit.Test;

public class ThreadTest {

    @Test
    public void getProperties() {
        System.out.println(Thread.currentThread().getId());
        System.out.println(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().getPriority());
        System.out.println(Thread.currentThread().getState());
    }

    @Test
    public void dumpStack() {
        Thread.dumpStack();
    }

    @Test
    public void getStack() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement i : stackTrace) {
            System.out.println(i.getClassName() + "." + i.getMethodName() + "()." + i.getLineNumber());
        }
    }

    @Test
    public void getState() {
        System.out.println(Thread.currentThread().getState());
    }

    @Test
    public void holdsLock() {
        Object obj = new Object();
        System.out.println(Thread.holdsLock(obj));
        synchronized (obj) {
            System.out.println(Thread.holdsLock(obj));
        }
        System.out.println(Thread.holdsLock(obj));
    }

    @Test
    public void yield() {
        System.out.println("start...");
        long start = System.currentTimeMillis();
        Thread.yield();
        System.out.println(System.currentTimeMillis() - start);
        System.out.println("end...");
    }

    @Test
    public void join() throws InterruptedException {
        System.out.println("start...");
        Runnable run = () -> {
            System.out.println("thread start...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread end...");
        };
        Thread t = new Thread(run);
        t.start();
        t.join();
        System.out.println("end...");
    }


}
